<?php
require_once(APPPATH.'views/components/page.php');
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<link href='<?php echo base_url() ?>resources/css/admin.css' rel='stylesheet' type='text/css' />
		<script src="<?php echo base_url() ?>resources/js/jquery-1.4.2.min.js"></script>
		<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
		
		<script type="text/javascript">
			function onSaveBtnClicked()
			{
				var markName = document.getElementById("markname").value;
				markName = markName.trim();
				
				if (markName.length == 0)
				{
					alert("Vui lòng nhập tên Nhãn sản phẩm");
					return false;
				}
				
				return true;
			}
			
			function onCancelBtnClicked(baseurl)
			{
				window.location = baseurl + "mark";
			}
			
			window.onbeforeunload = leavingPageHandler;
		    function leavingPageHandler(e)
		    {
		         var selectall = document.getElementById("selectall");
		         selectall.checked = false;
		         selectAll(selectall);
		    }
		
			function selectAll(selectall)
			{
				var checkboxes = document.getElementsByName("marks[]");
				for (var i = 0; i < checkboxes.length; i++)
				{
					checkboxes[i].checked = selectall.checked;
				}
				
				showDeleteBtn(selectall.checked);
			}
			
			function showDeleteBtn(show)
			{
				var deleteBtn = document.getElementById("deletebtn");
				deleteBtn.disabled = !show;
			}
			
			function onSingleCbClicked()
			{
				var checkboxes = document.getElementsByName("marks[]");
				var count = 0;
				for (var i = 0; i < checkboxes.length; i++)
				{
					if (checkboxes[i].checked)
						count++;
				}
				showDeleteBtn(count > 0);
				
				var selectall = document.getElementById('selectall');
				selectall.checked = (count == checkboxes.length);
					
			}
			
			function onDeleteBtnClicked()
			{
				var checkboxes = document.getElementsByName("marks[]");
				var count = 0;
				
				for (var i = 0, size = checkboxes.length; i < size; i++)
				{
					if (checkboxes[i].checked)
						count++;
				}
				
				if (count == 0)
					return false;
					
				return confirm("Xóa " + count + " Nhãn sản phẩm?");
			}
		</script>
		
		<title>Quản lý nhãn sản phẩm</title>
	</head>
	
	<body>
		<div id='wrapper'>
			<div id='header'>
				<div id="leftTopPan">
				<a href='<?php echo base_url() ?>'><img src='<?php echo base_url() ?>resources/images/logo.gif' title='Consultant' alt='Consultant' width='160' height='39' border='0' /></a>
				</div>
				<h1></h1>
			</div>
			<div id="middle">
				<div id="container">
					<div id="content">
						<br /><h2 align="center">Quản lý nhãn sản phẩm</h2><br />
						
						<?php
						
						// Error handler
						$error = $this->session->userdata('error');
						$this->session->unset_userdata('error');
						$errorText = "";
						
						switch ($error) {
							case INSERT_ERROR:
								$errorText = sprintf(INSERT_ERROR_TEXT, 'Nhãn sản phẩm');
								break;
							case DELETE_ERROR:
								$errorText = sprintf(DELETE_ERROR_TEXT, 'Nhãn sản phẩm');
								break;
							case EXISTED_ERROR:
								$name = $this->session->userdata('markname');
								$this->session->unset_userdata('markname');
								$errorText = sprintf(EXISTED_ERROR_TEXT, "Nhãn sản phẩm", $name);
								break;
							default:
								break;
						}
						?>
						<h3 style="color: red"><?php echo $errorText ?></h3>
						<?php
						
						
						$userdata = $this->session->userdata('userdata');
						
						if (Page::getInstance()->isAdmin())
						{
							// User information
							Page::getInstance()->generateUserInfo($userdata);
							
							// Begin insert form
							echo form_open('mark/insert');
							
							// Text box
							$titleTxt = array(	'id'		=>	'markname',
												'name'		=>	'markname');
							$showall = array(	'id'		=>	'showall',
												'name'		=>	'showall',
												'checked'	=>	FALSE,
												'value'		=>	1);
						?>
						<p>Tên nhãn: <?php echo form_input($titleTxt) ?></p>
						<p>Hiện trên mọi trang: <?php echo form_checkbox($showall) ?></p>
							
						<?php
							// Save button
							$saveBtn = array(	'id'		=>	'savebtn',
												'name'		=> 	'savebtn',
												'value'		=>	'Lưu',
												'onclick'	=>	'return onSaveBtnClicked()');
							echo form_submit($saveBtn);
							
							// Reset button
							$cancelBtn = array(	'id'		=> 'cancelbtn',
												'name'		=> 'cancelbtn',
												'value'		=> 'Hủy',
												'onclick'	=> "onCancelBtnClicked('".base_url()."')");
							echo form_reset($cancelBtn);
							
							// End insert form
							echo form_close();
							
							// Begin delete form
							echo form_open('mark/delete');
						}
						
						// Marks list
						$selectall = array(		'id'		=>	'selectall',
												'onclick'	=>	'selectAll(this)',
												'checked'	=>	FALSE);
						?>
						<p><hr /></p>
						<h2 align="center">Danh sách nhãn sản phẩm</h2><br />
						
						<?php
						// Delete button
							$deleteBtn = array(		'id'		=>	'deletebtn',
													'name'		=>	'deletebtn',
													'value'		=>	'Xóa',
													'disabled'	=>	TRUE,
													'onclick'	=>	'return onDeleteBtnClicked()');
							echo form_submit($deleteBtn);
						?>
						
						<table id="markstable" border="1">
							<tr>
								<td width="2%"><?php echo form_checkbox($selectall) ?></td>
								<th>Tên</th>
								<th>Hiện trên mọi trang</th>
								<th>Sản phẩm</th>
							</tr>
						<?php
						foreach ($marks as $mark)
						{
							?>
							<tr>
								<td><?php echo form_checkbox('marks[]', $mark->getId(), FALSE, "onclick='onSingleCbClicked()'") ?></td>
								<td><?php echo anchor('mark/view/'.$mark->getId(), $mark->getTitle()) ?></td>
								<td>
									<?php
									if ($mark->getShowAll() == TRUE)
										echo 'Yes';
									else
										echo 'No'; 
									?>
								</td>
								<td><?php echo anchor('mark/viewproducts/'.$mark->getId(), 'Danh sách sản phẩm') ?></td>
							</tr>
							<?php
						}
						?>
						</table>
						<?php echo form_close() ?>
					</div>
				</div>
					<?php
						if (Page::getInstance()->isAdmin())
						{
							Page::getInstance()->generateAdminLeftMenu();
						}
					?>
			</div>
		</div>
		
	</body>
</html>